package org.msn.util;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class MapUtil {
    public synchronized static void cloneMap(Map<String, Object> resourceMap, Map<String, Object> targetMap) {
        targetMap.forEach((k, v) -> {
            resourceMap.put(k, v);
        });
    }

    public synchronized static <T> T createEntityFromMap(Map<String, Object> map, Class<T> clazz) {
        T t = null;
        try {
            t = clazz.getDeclaredConstructor().newInstance();

            for (String key : map.keySet()) {
                BeanUtils.setProperty(t, key, map.get(key));
            }

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return t;
    }
}
